<template>
  <div style="max-height: 70vh; overflow-y: auto">
    <a-table
      :columns="columns"
      :data-source="tableData"
      :scroll="{ y: '70vh' }"
      bordered
      :loading="loading"
      :pagination="false"
    >
      <span slot="efficiency" slot-scope="text">{{ (Number(text) * 100).toFixed(2) }}%</span>
      <a-space slot="CompletionRate" slot-scope="text, record">
        <span>{{ ((record.currentActualOutput / record.shiftTheoryOutput) * 100).toFixed(2) }}%</span>
      </a-space>
      <a-space slot="currentActualOutput" slot-scope="text, record">
        <span>{{ Number(text).toFixed(2) }}</span>
      </a-space>
      <a-space slot="currentTheoryOutput" slot-scope="text, record">
        <span>{{ Number(text).toFixed(2) }}</span>
      </a-space>
      <a-space slot="shiftTheoryOutput" slot-scope="text, record">
        <span>{{ Number(text).toFixed(2) }}</span>
      </a-space>
      <a-space slot="ia" slot-scope="text, record">
        <span>{{ Number(text).toFixed(2) }}</span>
      </a-space>
      <a-space slot="consumption10000" slot-scope="text, record">
        <span>{{ Number(text).toFixed(2) }}</span>
      </a-space>
    </a-table>
  </div>
</template>
<script>
export default {
  props: {
    devName: {
      type: String,
      default: ''
    },
    scrollY: {
      type: Number,
      default: 0
    },
    loading: {
      type: Boolean,
      default: true
    },
    tableData: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      columns: [
        {
          title: '品种',
          dataIndex: 'productTypeTxt',
          key: 'productType',
          sorter: (a, b) => {
            // 自定义排序函数
            const aVal = a.productType.toLowerCase()
            const bVal = b.productType.toLowerCase()
            let aNum = '',
              bNum = ''

            // 提取数字部分
            const aNumMatch = aVal.match(/\d+/)
            if (aNumMatch) {
              aNum = aNumMatch[0]
            }
            const bNumMatch = bVal.match(/\d+/)
            if (bNumMatch) {
              bNum = bNumMatch[0]
            }

            // 先比较字母部分
            if (aVal.replace(/\d+/, '') !== bVal.replace(/\d+/, '')) {
              return aVal.localeCompare(bVal)
            }

            // 如果字母部分相同，再比较数字部分
            if (aNum !== bNum) {
              return parseInt(aNum, 10) - parseInt(bNum, 10)
            }

            // 如果都相同，则认为它们相等
            return 0
          }
        },
        {
          title: '车台号',
          dataIndex: 'deviceNo',
          key: 'deviceNo',
          sorter: (a, b) => Number(a.deviceNo) - Number(b.deviceNo)
        },
        {
          title: '效率(%)',
          dataIndex: 'efficiency',
          key: 'efficiency',
          scopedSlots: { customRender: 'efficiency' },
          sorter: (a, b) => Number(a.efficiency) - Number(b.efficiency)
        },
        {
          title: '当前实际产量(kg)',
          dataIndex: 'currentActualOutput',
          key: 'currentActualOutput',
          scopedSlots: { customRender: 'currentActualOutput' },
          sorter: (a, b) => Number(a.currentActualOutput) - Number(b.currentActualOutput)
        },
        {
          title: '当前理论产量(kg)',
          dataIndex: 'currentTheoryOutput',
          key: 'currentTheoryOutput',
          scopedSlots: { customRender: 'currentTheoryOutput' },
          sorter: (a, b) => Number(a.currentTheoryOutput) - Number(b.currentTheoryOutput)
        },
        {
          title: '整班理论产量(kg)',
          dataIndex: 'shiftTheoryOutput',
          key: 'shiftTheoryOutput',
          scopedSlots: { customRender: 'shiftTheoryOutput' },
          sorter: (a, b) => Number(a.shiftTheoryOutput) - Number(b.shiftTheoryOutput)
        },
        {
          title: '完成率',
          dataIndex: 'CompletionRate',
          key: 'CompletionRate',
          scopedSlots: { customRender: 'CompletionRate' },
          sorter: (a, b) =>
            Number(a.currentActualOutput) / Number(b.shiftTheoryOutput) -
            Number(b.currentActualOutput) / Number(a.shiftTheoryOutput)
        },
        {
          title: '当前长度',
          dataIndex: 'length',
          key: 'length',
          sorter: (a, b) => Number(a.length) - Number(b.length)
        },
        {
          title: '当前电流(安)',
          dataIndex: 'ia',
          key: 'ia',
          scopedSlots: { customRender: 'ia' },
          sorter: (a, b) => Number(a.ia) - Number(b.ia)
        },
        {
          title: '当班电耗(千瓦时)',
          dataIndex: 'shiftConsumption',
          key: 'shiftConsumption',
          sorter: (a, b) => Number(a.shiftConsumption) - Number(b.shiftConsumption)
        },
        {
          title: '万米电耗（度/万米）',
          dataIndex: 'consumption10000',
          key: 'consumption10000',
          scopedSlots: { customRender: 'consumption10000' },
          sorter: (a, b) => Number(a.consumption10000) - Number(b.consumption10000)
        }
      ]
    }
  },
  methods: {}
}
</script>

<style lang="less" scoped></style>
